//
//  PostMediaCell.m
//  CKApp
//
//  Created by CC on 2020/9/12.
//  Copyright © 2020 This Moment. All rights reserved.
//

#import "PostMediaCell.h"
#import "CCCollectionAdapter.h"
#import "PostMediaItemCellData.h"
#import "PostingAuxiliary.h"
#import "PostAddMediaItemCellData.h"
#import "LcFoundation.h"

@interface PostMediaCell ()
@property(nonatomic, weak) IBOutlet UICollectionView* collectionView;
@property(nonatomic, strong) CCCollectionAdapter* cellAdapter;

@end

@implementation PostMediaCell

- (void)postContruct {
  [super postContruct];
  self.cellAdapter = [[CCCollectionAdapter alloc] init];
  [self.cellAdapter registerCollectionView:self.collectionView
                           scrollDirection:UICollectionViewScrollDirectionVertical];
  self.cellAdapter.minimumLineSpacing = @[ @(5.0) ];
  self.cellAdapter.minimumInteritemSpacing = @[ @(5.0) ];
}

- (void)refreshView:(NSArray<CCMediaMeta*>*)metas
    deleteClickedHandler:(OnCollectionCellClickedHanlder)deleteClickedHandler
       addClickedHandler:(OnCollectionCellClickedHanlder)addClickedHandler {
  NSMutableArray<CCCollectionViewCellData*>* cellDatas =
      [NSMutableArray arrayWithCapacity:metas.count];
  for (CCMediaMeta* mediaMeta in metas) {
    PostMediaItemCellData* itemData = [PostMediaItemCellData cellDataWithMeta:mediaMeta
                                                         deleteClickedHandler:deleteClickedHandler];
    [cellDatas addObject:itemData];
  }
  if (metas.count < 9) {
    PostAddMediaItemCellData* itemData = [[PostAddMediaItemCellData alloc] init];
    CGFloat itemWidth = (LcUIUtility.screenWidth - 36.0 - 10.0) / 3.0;
    itemData.cellSize = CGSizeMake(itemWidth, itemWidth);
    itemData.onCellClickedHandler = addClickedHandler;
    [cellDatas addObject:itemData];
  }
  self.cellAdapter.cellDatas = [NSMutableArray arrayWithArray:@[ cellDatas ]];
  [self.cellAdapter autoRegisterCellByCellData:self.collectionView];
  [self.cellAdapter refreshAllCells];
}

@end
